﻿using QuanLyBanHang.BLL;
using QuanLyBanHang.DTO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLyBanHang
{
    public partial class frmKhachHang : Form
    {
        KhachHangDTO kh = new KhachHangDTO();
        public frmKhachHang()
        {
            InitializeComponent();
        }

        private void frmKhachHang_Load(object sender, EventArgs e)
        {
            KhoaDieuKien();
            dataGridView1.DataSource = KhachHangBLL.LayTatCaThongTinKhachHang(kh);
        }
      
        #region mo,khoa dieu kien ,TaoKH,kiemtrathemmoi
        //kiem tra dieu kien luu la cua them moi hay cua sua
        bool themmoi;
        public void SetNull()
        {
           textBox1.Text= "";
           textBox2.Text = "";
           textBox3.Text = "";
           textBox4.Text = "";
           comboBox2.Text = "Khach Lẻ";
           
        }
        public void KiemTraDuLieu()
        {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "")
            {
                MessageBox.Show("Xin moi nhap du lieu");

            }
        }
        public void KhoaDieuKien()
        {
            textBox1.Enabled = false;
            textBox2.Enabled = false;
            textBox3.Enabled = false;
            textBox4.Enabled = false;
            comboBox2.Enabled = false;

            button1.Enabled = true;
            button2.Enabled = true;
            button3.Enabled = true;
            button4.Enabled = false;

        }
        public void MoDieuKien()
        {
            textBox1.Enabled = true;
            textBox2.Enabled = true;
            textBox3.Enabled = true;
            textBox4.Enabled = true;
            comboBox2.Enabled = true;

            button1.Enabled = false;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = true;

        }
        #endregion
        //them
        private void button1_Click(object sender, EventArgs e)
        {
            MoDieuKien();
            SetNull();
            themmoi = true;
           
        }
    

     
        //sua
        private void button2_Click(object sender, EventArgs e)
        {
           
            MoDieuKien();
            textBox1.Enabled = false;
            themmoi = false;
           // frmKhachHang_Load(sender, e);
        }
        //xoa
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                KhachHangDTO.MaKhachHang = textBox1.Text;
                KhachHangBLL.XoaKhachHang(kh);
                MessageBox.Show("Xoa Thanh Cong");
                KhoaDieuKien();
                frmKhachHang_Load(sender, e);
                SetNull();
            }
            catch 
            {

                MessageBox.Show("loi khong the xoa");
            }
           
          
        }
       
        //luu
        private void button4_Click(object sender, EventArgs e)
        {
            KiemTraDuLieu();
            if (themmoi == true)
            {
                try
                {
                    KhachHangDTO.MaKhachHang = textBox1.Text;
                    KhachHangDTO.HoTen = textBox2.Text;
                    KhachHangDTO.DiaChi = textBox3.Text;
                    KhachHangDTO.DienThoai = textBox4.Text;
                    KhachHangDTO.LoaiKhachHang = comboBox2.Text;
                    KhachHangBLL.ThemKhachHang(kh);
                    MessageBox.Show("Them moi thanh cong");
                    
                }
                catch 
                {

                    MessageBox.Show("Lỗi");
                    return;
                }
                
            }
            else
            {
                try
                {
                    KhachHangDTO.MaKhachHang = textBox1.Text;
                    KhachHangDTO.HoTen = textBox2.Text;
                    KhachHangDTO.DiaChi = textBox3.Text;
                    KhachHangDTO.DienThoai = textBox4.Text;
                    KhachHangDTO.LoaiKhachHang = comboBox2.Text;
                    KhachHangBLL.SuaKhachHang(kh);
                    MessageBox.Show("Sua thanh cong");
                   
                }
                catch
                {

                    MessageBox.Show("Lỗi");
                    return;
                }
            }
            SetNull();
            frmKhachHang_Load(sender, e);
            KhoaDieuKien();
        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            KhoaDieuKien();
            int i = e.RowIndex;
            textBox1.Text = dataGridView1.Rows[i].Cells["MaKH"].Value.ToString();
            textBox2.Text = dataGridView1.Rows[i].Cells["HoTenKH"].Value.ToString();
            textBox3.Text = dataGridView1.Rows[i].Cells["DiaChi"].Value.ToString();
            textBox4.Text = dataGridView1.Rows[i].Cells["DienThoai"].Value.ToString();
            comboBox2.Text = dataGridView1.Rows[i].Cells["LoaiKH"].Value.ToString();

        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát hay không ", "ThôngBáo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                this.Close();
            }
        }
    }
}
